<?php

include 'base.php';
$pagesize = 8;

if($is_login == 0){
	echo "<script>";
	echo "alert('Please login or ontact us for new account.');";
	echo "location.href='index.php'";
	echo "</script>";
	die();
}

$url = "products.php";

$sql = "select id,ename,itemid,image,price from equipment ";
$cu = "?";

$where  = " 1=1 ";
//$subtotal = 0;

if (! empty ( $_GET ['sid'] ) && (int)$_GET['sid']) {
	$sid = (int)$_GET['sid'];
	$sub = $db->do_one_bysql("select istop,gname from games where id=$sid");
	if(!empty($sub['istop'])){
		$gid = $sub['istop'];
		$tpl->assign ( 'sub', $sub );
		$cu .= "sid=".$sid."&";
	}
	$where .= " and  sid=" . $sid;
} else {
	$gid = ! empty ( $_GET ['gid'] ) ? (int)$_GET['gid'] : 0;
	$doc = array();
	$sql2 = "select * from games where  status=1 and istop=$gid order by place,id desc ";
	$db->query($sql2);
	$c_games = array();
	
	while($row = $db->fetch_array()){
		if($row['id'] == $gid) $doc = $row;
		$c_games[] =  $row; 
		$subtotal++;
	}
}

$tpl->assign('c_games', $c_games);


if($subtotal == 0){
	
	if($gid){
		$doc = $db->do_one_bysql("select gname from games where id=$gid");
		$tpl->assign ( 'doc', $doc );
	
		$where .= " and  gid=" . $gid;
	}
	
	if(empty($sid) && $gid) $cu .= "gid=".$gid."&";
	
	$total = $db->do_count("equipment", $where);
	
	
	$page = ! empty ( $_GET['page'] )?(int) $_GET['page'] :1;
	
	$start = ($page - 1 ) * $pagesize;
	
	$outpages = numofpage2($total,$page, ceil($total/$pagesize), $url.$cu."page=");
	
	$tpl->assign('outpages',$outpages);
	
	
	$docs = $db->do_all_bysql ( $sql ." where ".$where ." order by place desc, id desc limit $start, $pagesize" );
	
	$tpl->assign ( 'docs', $docs );
	//pre($docs);
	
}
$tpl->assign ( 'subtotal', $subtotal );

$tpl->assign ( 'gid', $gid );
$tpl->assign ( 'sid', $sid );
$tpl->show("products");

?>